package com.drought.rained.presenter;

import com.drought.rained.BuildConfig;
import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.mvpview.OrderMvpViewLJ;
import com.drought.rained.response.CalculateResponseLJ;
import com.drought.rained.response.CheckOrderQualificationsResponseLJ;
import com.drought.rained.response.CreateOrderResponseLJ;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class OrderPresenterLJ extends BasePresenterLJ {
    private OrderMvpViewLJ mvpView;

    public OrderPresenterLJ(OrderMvpViewLJ mvpView) {
        this.mvpView = mvpView;
    }

    public void checkOrderQualifications() {
        toSubScribe(req.checkOrderQualifications(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(null)), new CustomConsumerLJ<CheckOrderQualificationsResponseLJ>() {
            @Override
            public void call(CheckOrderQualificationsResponseLJ checkOrderResponse) {
                mvpView.onGetDataNext(checkOrderResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void createOrder(String mBorrowMoney, String mPid, String userUuid, String addressBookUuid) {
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("expiryReturned", mBorrowMoney.trim());
        formMap.put("pId", mPid.trim());
        formMap.put("userUuid", userUuid);
        formMap.put("addressBookUuid", addressBookUuid);
        toSubScribe(req.createOrder(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(formMap)), new CustomConsumerLJ<CreateOrderResponseLJ>() {
            @Override
            public void call(CreateOrderResponseLJ response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void getCalculateData(String borrowMoney, String pId) {
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("borrowMoney", borrowMoney);
        formMap.put("pId", pId);
        toSubScribe(req.getCalculateData(UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(formMap)), new CustomConsumerLJ<CalculateResponseLJ>() {
            @Override
            public void call(CalculateResponseLJ response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

}
